// N1Math.cpp: implementation of the N1Math class.
//
//////////////////////////////////////////////////////////////////////

#include "N1Math.h"
#include "N1Lib.h"

uint32 N1GetNewSizeByLog2(uint32 currentSize, 
                          uint32 needToAdd, 
                          uint32 minSize)
{
    N1Assert( needToAdd!=0 );

    if (currentSize==0)
    {
        return N1Max(needToAdd, minSize);
    }

    return N1Max(currentSize + needToAdd, currentSize * 2);
}

uint32 N1GetNewSizeByLog2(uint32 currentSize, 
                          uint32 needToAdd, 
                          uint32 minSize,
                          uint32 maxGrow)
{
    N1Assert( needToAdd!=0 );
    N1Assert( maxGrow>4 );
    N1Assert( needToAdd<maxGrow );

    if (currentSize==0)
    {
        return N1Max(needToAdd, minSize);
    }

    uint32 growSize = N1Max(needToAdd, currentSize);

    if (growSize > maxGrow)
    {
        if (needToAdd>maxGrow)
        {
            N1ThrowException("Maximum grow size must be more than added size.");
        }

        growSize = maxGrow;
    }

    return currentSize + growSize;
}
